package com.pzhu.dao.impl;

import com.pzhu.dao.LoginDao;
import com.pzhu.pojo.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class LoginDaoImpl implements LoginDao {

    private PreparedStatement createAll(PreparedStatement ps, String sql) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp?&useSSL=false&useUnicode=true&&characterEncoding=utf8", "root", "0714");
            //创建SQL语句
            ps = conn.prepareStatement(sql);
            //执行SQL语句
            return ps;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ps;
    }

    private void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (ps != null) {
                ps.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public User checkUser(User userInPut) {
        //定义JDBC对象
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        //定义用户对象并初始化，存储查询结果
        User userSelect = null;
        try {

            String sql = "select pwd from t_user where uname=?;";
            //获取 PreparedPreparedStatement对象
            ps = createAll(ps, sql);
            ps.setString(1, userInPut.getUname());
            rs = ps.executeQuery();
            //遍历结果封装数据
            while (rs.next()) {
                userSelect = new User();
                userSelect.setPwd(rs.getString("pwd"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            closeAll(conn, ps, rs);
        }
        return userSelect;
    }

    @Override
    public User checkUserById(User userInPut) {
        //定义JDBC对象
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        //定义用户对象并初始化，存储查询结果
        User userSelect = null;
        try {

            String sql = "select uid from t_user where uid=?;";
            //获取 PreparedPreparedStatement对象
            ps = createAll(ps, sql);
            ps.setInt(1, userInPut.getUid());
            //执行SQL语句
            rs = ps.executeQuery();
            //遍历结果封装数据
            while (rs.next()) {
                userSelect = new User();
                userSelect.setUid(rs.getInt("uid"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            closeAll(conn, ps, rs);
        }
        return userSelect;
    }

    @Override
    public int selectUidByUName(String userName) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        //定义用户对象并初始化，存储查询结果
        User userSelect = null;
        try {
            String sql = "select uid from t_user where uname= ?;";
            //获取 PreparedPreparedStatement对象
            ps = createAll(ps, sql);
            ps.setString(1, userName);
            //执行SQL语句
            rs = ps.executeQuery();
            //遍历结果封装数据
            while (rs.next()) {
                userSelect = new User();
                userSelect.setUid(rs.getInt("uid"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            closeAll(conn, ps, rs);
        }
        return userSelect!=null?userSelect.getUid():null;
    }

    @Override
    public String selectUNameByUid(int userId) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        //定义用户对象并初始化，存储查询结果
        User userSelect = null;
        try {
            String sql = "select uname from t_user where uid=?;";
            //获取 PreparedPreparedStatement对象
            ps = createAll(ps, sql);
            ps.setInt(1, userId);

            //执行SQL语句
            rs = ps.executeQuery();
            //遍历结果封装数据
            while (rs.next()) {
                userSelect = new User();
                userSelect.setUname(rs.getString("uname"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            closeAll(conn, ps, rs);
        }
        return userSelect!=null?userSelect.getUname():null;
    }

    @Override
    public User selectUserById(User userInPut) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        //定义用户对象并初始化，存储查询结果
        User userSelect = null;
        try {
            String sql = "select * from t_user where uid=?;";
            //获取 PreparedPreparedStatement对象
            ps = createAll(ps, sql);
            ps.setInt(1, userInPut.getUid());

            //执行SQL语句
            rs = ps.executeQuery();
            //遍历结果封装数据
            while (rs.next()) {
                userSelect = new User();
                userSelect.setUid(rs.getInt("uid"));
                userSelect.setUname(rs.getString("uname"));
                userSelect.setPwd(rs.getString("pwd"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            closeAll(conn, ps, rs);
        }
        return userSelect;
    }

}
